package com.ucode.crm.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ucode.crm.mapper.ContactsMapper;
import com.ucode.crm.mapper.CustomerMapper;
import com.ucode.crm.mode.Contacts;
import com.ucode.crm.mode.Customer;
import com.ucode.crm.service.ContactsService;
import com.ucode.tool.util.IdentifyUtils;

@Service
public class ContactsServiceImpl implements ContactsService {
    
    @Autowired
    private ContactsMapper contactsMapper;
    
    @Autowired
    private CustomerMapper customerMapper;
    
    @Override
    @Transactional(rollbackFor=Exception.class)
    public Contacts insert(Contacts contacts) {
        contacts.setId(IdentifyUtils.getDefaultSnowflakeId());
        Date now = new Date();
        contacts.setCreatedTime(now);
        contacts.setUpdatedTime(now);
        contactsMapper.insert(contacts);
        return contacts;
    }
    
    @Override
    public int update(Contacts contacts) {
        Contacts old = contactsMapper.findById(contacts.getId());
        if(old != null && old.getDc().equals(0)){
            Customer customer = new Customer();
            customer.setId(contacts.getCustomerId());
            customer.setContactsId(contacts.getId());
            customer.setMobile(contacts.getMobile());
            customer.setTelephone(contacts.getTelephone());
            customer.setCname(contacts.getName());
            customer.setSex(contacts.getSex());
            customer.setBirthday(contacts.getBirthday());
            customerMapper.update(customer);
        }
        return contactsMapper.update(contacts);
    }

    @Override
    public int updateDc(Long id) {
        Contacts contacts = contactsMapper.findById(id);
        int i = 0;
        if(contacts != null && !contacts.getDc().equals(0)){
            //首要联系人
            Customer customer = customerMapper.findById(contacts.getCustomerId());
            contactsMapper.updateDc(customer.getContactsId(), 1);
            i = contactsMapper.updateDc(contacts.getId(), 0);
            if(i > 0){
                customer.setContactsId(contacts.getId());
                customer.setMobile(contacts.getMobile());
                customer.setTelephone(contacts.getTelephone()== null ? "" : contacts.getTelephone());
                customer.setCname(contacts.getName()== null ? "" : contacts.getName());
                customer.setSex(contacts.getSex()== null ? 0 : contacts.getSex());
                customer.setBirthday(contacts.getBirthday());
                customerMapper.update(customer);
            }
        }
        return i;
    }

    @Override
    public int deleteByLogic(Long id) {
        return 0;
    }

    @Override
    public Contacts findById(Long id) {
        return contactsMapper.findById(id);
    }

    @Override
    public List<Contacts> findByIds(List<Long> ids) {
        return contactsMapper.findByIds(ids);
    }

    @Override
    public List<Contacts> findListByCustomerId(Long customerId) {
        return contactsMapper.findListByCustomerId(customerId);
    }

}
